/**
 * @author shahma
 */
dojo.provide("medryx.orm.Association");
dojo.require("medryx.orm.Property");

/**
 *  ARGUMENTS:
 *  associationClass:class className
 *  readonly:true|FALSE //if false, then automatic dirty checking is performed on this property. and a setter is created
 *  lazy: TRUE | false //if false then if this property is not initialized at object creation time, it is fetched immediately. o
 * 			  otherwise, it is only fetched when a call to the property's accessor is made
 */
dojo.declare("medryx.orm.Association", medryx.orm.Property, {
	constructor:function() {
		if (!this.associationClass) {
			console.error(this);
			throw new medryx.exceptions.IllegalArgumentException("must specify associationClass for association for: " + this.propertyName);
		} 
	},
	
    getValueToAssign:function(value) {
		var propToAssign = null;
		if (typeof value === "undefined") {
			propToAssign = this;
		} else if (value === null || value === 0 || value === "") {
			propToAssign = null;
		 }else {
			propToAssign = this.entityManager.buildEntity(this.associationClass, value);
		}
		return propToAssign;
	},
	initialize:function(instance, propertyName, value) {
		this.set(instance, propertyName, this.getValueToAssign(value));
		return instance[propertyName];
	}
});

